﻿using System;
using System.Web.Mvc;

namespace Df.Web.Mvc
{

    public abstract class DfBaseController : Controller
    {
        #region BaseMethod
        // Indicate if it is IframeMode 
        public bool IsIframeMode
        {
            get
            {
                return (Request["iframeMode"] == "true" ? true : false);
            }
        }

        protected override IAsyncResult BeginExecute(System.Web.Routing.RequestContext requestContext, AsyncCallback callback, object state)
        {
            // ViewBag.Mode fill changing layout
            if (requestContext.HttpContext.Request["iframeMode"] != null || requestContext.RouteData.Values["iframeMode"] != null)
            {
                ViewBag.IframeMode = "true";
            }

            //try
            //{                
            //    Session["CurrentRoleName"] = CurrentUserRole();
            //}
            //catch (Exception)
            //{
                    
            //}

            return base.BeginExecute(requestContext, callback, state);
        }

        #endregion

        public abstract string CurrentUserRole();

        #region GeneratePdf

        protected ActionResult Pdf()
        {
            return Pdf(null, null, null);
        }

        protected ActionResult Pdf(string fileDownloadName)
        {
            return Pdf(fileDownloadName, null, null);
        }

        protected ActionResult Pdf(string fileDownloadName, string viewName)
        {
            return Pdf(fileDownloadName, viewName, null);
        }

        protected ActionResult Pdf(object model)
        {
            return Pdf(null, null, model);
        }

        protected ActionResult Pdf(string fileDownloadName, object model)
        {
            return Pdf(fileDownloadName, null, model);
        }
        protected ActionResult Pdf(string fileDownloadName, string viewName, object model)
        {
            return Pdf(fileDownloadName, viewName, model, string.Empty);
        }
        protected ActionResult Pdf(string fileDownloadName, string viewName, object model, string headerTitle)
        {
            // Based on View() code in Controller base class from MVC
            if (model != null)
            {
                ViewData.Model = model;
            }
            PdfResult pdf = new PdfResult()
            {
                FileDownloadName = fileDownloadName,
                HeaderTitle = headerTitle,
                ViewName = viewName,
                ViewData = ViewData,
                TempData = TempData,
                ViewEngineCollection = ViewEngineCollection
            };
            return pdf;
        }

        #endregion
    }
}